#!/usr/bin/env python
# encoding: utf-8
# Date: 2017-06-18 17:09:08
# file:views.py
# Email: wang521kang@outlook.com
# Author: 王康
from commodity.models import Commodity, CommodityProperty, CommodityPropertyValue, CommodityImg
from utils.views import request_parms_check, get_json


@request_parms_check(classification_id=('商品分类', 'r,int'))
def query_classification_commodity(request, classification_id):
    l = []
    query = Commodity.objects.filter(commodity_classification=classification_id).all()
    for obj in query:
        res = {}
        res['id'] = obj.id
        res['name'] = obj.name
        res['price'] = obj.price
        res['preferential'] = obj.preferential
        res['preferential'] = obj.preferential
        query_img_num = CommodityImg.objects.filter(commodity_id=obj.id, is_active=True).count()
        query_com_img = CommodityImg.objects.filter(commodity_id=obj.id, is_active=True)
        if query_img_num > 0:
            res['img_url'] = query_com_img[0].img_url
        query_property = CommodityProperty.objects.filter(commodity_id=obj.id)
        for item in query_property:
            res['property'] = item.property
            query_property_value = CommodityPropertyValue.objects.filter(commodity_property_id=item.id)
            for val in query_property_value:
                res['value'] = val.value
        l.append(res)
    return get_json(True, '', l)
